package org.example.service.Impl;

import org.example.dao.ReciteDao;
import org.example.entity.Recite;
import org.springframework.stereotype.Service;
import org.example.service.ReciteService;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ReciteServiceImpl implements ReciteService {

    @Resource
    ReciteDao reciteDao;


    @Override
    public List<Recite> getRecitedPoem(Long userId) {
        Map<String,Object> map=new HashMap<>();
        map.put("isRecite",1);
        map.put("userId",userId);
        List<Recite> recites = reciteDao.selectByMap(map);
        return recites;
    }

    @Override
    public List<Recite> getUnRecitedPoem(Long userId) {
        Map<String,Object> map=new HashMap<>();
        map.put("isRecite",0);
        map.put("userId",userId);
        List<Recite> recites = reciteDao.selectByMap(map);
        return recites;
    }

    @Override
    public Boolean isRecite(Long userId, Long poemId) {
        Map<String,Object> map=new HashMap<>();
        map.put("userId",userId);
        map.put("poemId",poemId);
        List<Recite> recites = reciteDao.selectByMap(map);
        if (recites.size()==0){
            return false;
        }
        return true;
    }

    @Override
    public Boolean isRecited(Long userId, Long poemId) {
        Map<String,Object> map=new HashMap<>();
        map.put("isRecite",1);
        map.put("poemId",poemId);
        map.put("userId",userId);
        List<Recite> recites = reciteDao.selectByMap(map);
        if (recites.size()==0){
            return false;
        }
        return true;
    }

    @Override
    public Boolean addRecite(Recite recite) {
        return reciteDao.insert(recite)>0;
    }

    @Override
    public Boolean deleteRecite(Long userId, Long poemId) {
        Map<String,Object> map=new HashMap<>();
        map.put("userId",userId);
        map.put("poemId",poemId);
        return  reciteDao.deleteByMap(map)>0;
    }

    @Override
    public Boolean updateRecite(Recite recite) {
        deleteRecite(recite.getUserId(),recite.getPoemId());
        return addRecite(recite);
        /*return reciteDao.updateById(recite)>0;*/
    }
}

